<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php');
require_once('../../../include/inc_config.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?Identifiant_condition_indemnisation=" . htmlentities($_GET['Identifiant_condition_indemnisation']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")){
//$bool=verifLibelle1('Code_condition_indemnisation') &&  verifLibelle('Libelle_condition_indemnisation') && verifLibelle('Identifiant_offre')&&  verifNumeric($_POST['Valeur_condition_indemnisation']) && dateValide('Date_debut_condition_indemnisation')&&(!empty($_POST['Date_fin_condition_indemnisation'])&& dateValide($_POST['Date_fin_condition_indemnisation']));
$bool=verifLibelle1('Code_condition_indemnisation') &&  verifLibelle('Libelle_condition_indemnisation') && verifLibelle('Identifiant_offre') && dateValide('Date_debut_condition_indemnisation')&&(!empty($_POST['Date_fin_condition_indemnisation'])&& dateValide($_POST['Date_fin_condition_indemnisation']));
	if(!empty($_POST['Date_fin_condition_indemnisation']))$bool=$bool && dateValide($_POST['Date_fin_condition_indemnisation']);

//$bool1=verifNumeric($_POST['Valeur_condition_indemnisation']);}

if(isset($_POST["MM_update"])&& !$bool) 
{
if(isset($_POST['Date_fin_condition_indemnisation']) && $_POST['Date_debut_condition_indemnisation']){
	$datedebut = datevalider($_POST['Date_debut_condition_indemnisation']);
		$datefin = datevalider($_POST['Date_fin_condition_indemnisation']);
	$datevalide1 = datevalide($_POST['Date_debut_condition_indemnisation']);
		$datevalide2 = datevalide($_POST['Date_fin_condition_indemnisation']);
	$lesdates=verifDate($_POST['Date_debut_condition_indemnisation'],$_POST['Date_fin_condition_indemnisation']);
}

// --- la modification concerne autre chose que le type
$oConditionIndemnisation = new dataObject_conditionIndemnisation($_POST['Identifiant_condition_indemnisation']);
$iUpdateAutre = $oConditionIndemnisation->_CODE_CDT_INDEMN == $_POST['Code_condition_indemnisation'] ;
$iUpdateAutreQueGarantie = $iUpdateAutre && $oConditionIndemnisation->_IDENTIFIANT_GARANTIE == $_POST['Identifiant_garantie'] ;
$iUpdateAutreQueOffre = $iUpdateAutre && $oConditionIndemnisation->_IDENTIFIANT_OFFRE == $_POST['Identifiant_offre'] ;

if (!$iUpdateAutreQueGarantie)
{
    // --- si le code existe pour l'offre
    $toCriterias['Code_condition_indemnisation'] = $_POST['Code_condition_indemnisation'] ;
    $toCriterias['Identifiant_garantie'] = $_POST['Identifiant_garantie'] ;
    $existeGarantie = existeEnregistrement($database_microassur, $microassur, "conditionindemnisation", $toCriterias) ;
}
else
{
    $existeGarantie = FALSE ;
}

if (!$iUpdateAutreQueOffre)
{
    // --- si le code existe pour le produit
    $toCriterias['Identifiant_offre'] = $_POST['Identifiant_offre'] ;
    unset($toCriterias['Identifiant_garantie']) ;
    $existeOffre = existeEnregistrement($database_microassur, $microassur, "conditionindemnisation", $toCriterias) ;
}
else
{
    $existeOffre = FALSE ;
}

$bExistes = $existeGarantie && $existeOffre ;

if(isset($_POST["MM_update"]) && $existeGarantie ) $message = colorer("Ce code existe déjà pour la garantie choisie","false");
elseif(isset($_POST["MM_update"]) && $existeOffre ) $message = colorer("Ce code existe déjà pour l'offre choisie","false");
elseif(empty($_POST['Code_condition_indemnisation'])) $message = colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_condition_indemnisation')) $message = colorer("Le code saisi comporte des caractères spéciaux interdits ","false");

elseif(empty($_POST['Libelle_condition_indemnisation'])) $message = colorer(" Vous devez saisir le libellé","false");
elseif(!verifLibelle('Libelle_condition_indemnisation')) $message = colorer("Le libellé saisi comporte des caractères spéciaux interdits ","false");
// --- elseif(empty($_POST['Valeur_condition_indemnisation'])) $message = colorer(" Vous devez saisir la valeur de la condition d’indemnisation","false");
// elseif(!verifNumeric($_POST['Valeur_condition_indemnisation'])) $message = colorer("La valeur de la condition d’indemnisation doit être numérique","false");

elseif(empty($_POST['Date_debut_condition_indemnisation'])) $message = colorer("Vous devez saisir la date de début","false");

elseif(isset($_POST['Date_debut_condition_indemnisation']) && !$datedebut){
	$message = colorer("Le format de la date de début est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_debut_condition_indemnisation']) && !$datevalide1){
	$message = colorer("La date de début est incorrecte ","false");
}elseif(isset($_POST['Date_fin_condition_indemnisation']) && $_POST['Date_fin_condition_indemnisation']!=NULL && !$datefin){
	$message = colorer("Le format de la date de fin est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_fin_condition_indemnisation']) && $_POST['Date_fin_condition_indemnisation']!=NULL && !$datevalide2){
	$message = colorer("La date de fin est incorrecte ","false");
}
elseif(isset($_POST['Date_debut_condition_indemnisation']) && isset($_POST['Date_fin_condition_indemnisation'])  && !empty($_POST['Date_fin_condition_indemnisation']) && !empty($_POST['Date_debut_condition_indemnisation'])&& !$lesdates)
	{$message = colorer("La date de fin doit être postérieure à la date de début","false"); }
	else
		{
		  $updateSQL = sprintf("UPDATE conditionindemnisation SET Code_condition_indemnisation=%s, Libelle_condition_indemnisation=%s, Valeur_condition_indemnisation=%s, Type_condition_indemnisation=%s, Date_debut_condition_indemnisation=%s, Date_fin_condition_indemnisation=%s, Identifiant_garantie=%s, Identifiant_offre=%s WHERE Identifiant_condition_indemnisation=%s",
							   GetSQLValueString($_POST['Code_condition_indemnisation'], "text"),
							   GetSQLValueString($_POST['Libelle_condition_indemnisation'], "text"),
							   GetSQLValueString($_POST['Valeur_condition_indemnisation'], "int"),
							   GetSQLValueString($_POST['Type_condition_indemnisation'], "text"),
							   GetSQLValueString(dateEN2FR($_POST['Date_debut_condition_indemnisation']), "date"),
							   GetSQLValueString(dateEN2FR($_POST['Date_fin_condition_indemnisation']), "date"),
							   GetSQLValueString($_POST['Identifiant_garantie'], "int"),
							   GetSQLValueString($_POST['Identifiant_offre'], "int"),
							   GetSQLValueString($_POST['Identifiant_condition_indemnisation'], "int"));

		  mysql_select_db($database_microassur, $microassur);
		  mysql_query("START TRANSACTION");

		  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());

		if($Result1) 
		{ mysql_query("commit");$message=colorer("La modification de la condition indemnisation est prise en compte","true");
		}
		else { mysql_query("rollback");$message=colorer("Contacter la DSI, erreur modification condition indemnisation","false");}  

		}
	}
}
$colname_rs = "-1";
if (isset($_GET['Identifiant_condition_indemnisation'])) {
  $colname_rs = $_GET['Identifiant_condition_indemnisation'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs = sprintf("SELECT * FROM conditionindemnisation WHERE Identifiant_condition_indemnisation = %s", GetSQLValueString($colname_rs, "int"));
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rsgaranti = "SELECT * FROM garantie";
$rsgaranti = mysql_query($query_rsgaranti, $microassur) or die(mysql_error());
$row_rsgaranti = mysql_fetch_assoc($rsgaranti);
$totalRows_rsgaranti = mysql_num_rows($rsgaranti);

mysql_select_db($database_microassur, $microassur);
$query_rsoffre = "SELECT * FROM offre";
$rsoffre = mysql_query($query_rsoffre, $microassur) or die(mysql_error());
$row_rsoffre = mysql_fetch_assoc($rsoffre);
$totalRows_rsoffre = mysql_num_rows($rsoffre);
/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0003"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../condition.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Condition indemnisation";
					$nommodule2="Détail condition indemnisation";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
	<div class="corps1">
		<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft"></div>
			<div class="imgmiddle">
				<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
				<div class="btn1 btnr"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
					<div class="content_tableform1">
							  <table>
								<tr>
								  <td>CODE</td>
								  <td><input class="resize_1" type="text" maxlength="5" name="Code_condition_indemnisation" value="<?php if(isset($_POST['Code_condition_indemnisation']))echo remind_text('Code_condition_indemnisation'); else echo htmlentities($row_rs['Code_condition_indemnisation'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
								</tr>
								<tr>
								  <td>LIBELLE</td>
								  <td colspan="3"><input class="td2" type="text" name="Libelle_condition_indemnisation" value="<?php if(isset($_POST['Libelle_condition_indemnisation']))echo remind_text('Libelle_condition_indemnisation'); else echo htmlentities($row_rs['Libelle_condition_indemnisation'], ENT_COMPAT, 'utf-8'); ?>" size="32"  maxlength="30"/></td>
								</tr>
								<tr>
								  <td>VALEUR</td>
								  <td colspan="3"><input class="td1" type="text" name="Valeur_condition_indemnisation" value="<?php if(isset($_POST['Valeur_condition_indemnisation']))echo remind_text('Valeur_condition_indemnisation'); else echo htmlentities($row_rs['Valeur_condition_indemnisation'], ENT_COMPAT, 'utf-8'); ?>" size="32"  maxlength="10"/></td>
								</tr>
								<tr>
								  <td>DATE DEBUT</td>
								  <td><input name="Date_debut_condition_indemnisation" type="text" class="date" value="<?php if(isset($_POST['Date_debut_condition_indemnisation']))echo $_POST['Date_debut_condition_indemnisation']; else echo htmlentities(dateEN2FR($row_rs['Date_debut_condition_indemnisation']), ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="10" /> </td>
								  <td class="td">DATE FIN</td>
								  <td><input type="text" name="Date_fin_condition_indemnisation" class="date" value="<?php if(isset($_POST['Date_fin_condition_indemnisation']))echo $_POST['Date_fin_condition_indemnisation']; else echo htmlentities(dateEN2FR($row_rs['Date_fin_condition_indemnisation']), ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="10" />  </td>
								</tr>
								<tr>
								  <td>TYPE</td>
								   <td colspan="3"><select class="select" name="Type_condition_indemnisation">
									<?php do{ ?>
												<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php if (!(strcmp($row_rs_dico2['Code_element'], htmlentities($row_rs['Type_condition_indemnisation'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_dico2['Nom_element']; ?></option>
									<?php 	} while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
									</select>
								  </td>
								</tr>
								<tr>
								  <td>GARANTIE</td>
								  <td><select name="Identifiant_garantie"><option value="" <?php if (!(strcmp("", htmlentities($row_rs['Identifiant_garantie'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>>Sélectionner une garantie</option>
									  <?php
							do {  
							?>
									  <option value="<?php echo $row_rsgaranti['Identifiant_garantie']?>"<?php if (!(strcmp($row_rsgaranti['Identifiant_garantie'], htmlentities($row_rs['Identifiant_garantie'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsgaranti['Libelle_garantie']?></option>
									  <?php
							} while ($row_rsgaranti = mysql_fetch_assoc($rsgaranti));
							  $rows = mysql_num_rows($rsgaranti);
							  if($rows > 0) {
								  mysql_data_seek($rsgaranti, 0);
								  $row_rsgaranti = mysql_fetch_assoc($rsgaranti);
							  }
							?>
										</select></td>

								  <td class="td">OFFRE</td>
								  <td><select name="Identifiant_offre">
									  <?php
							do {  
							?>
									  <option value="<?php echo $row_rsoffre['Identifiant_offre']?>"<?php if (!(strcmp($row_rsoffre['Identifiant_offre'], htmlentities($row_rs['Identifiant_offre'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsoffre['Libelle_offre']?></option>
									  <?php
							} while ($row_rsoffre = mysql_fetch_assoc($rsoffre));
							  $rows = mysql_num_rows($rsoffre);
							  if($rows > 0) {
								  mysql_data_seek($rsoffre, 0);
								  $row_rsoffre = mysql_fetch_assoc($rsoffre);
							  }
							?>
								  </select>      </td>
								</tr>
							  </table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="./"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
  <input type="hidden" name="MM_update" value="form1" />
  <input type="hidden" name="Identifiant_condition_indemnisation" value="<?php echo $row_rs['Identifiant_condition_indemnisation']; ?>" />
</form>
	</div>
</body>
</html>
<?php
mysql_free_result($rs);
mysql_free_result($rsgaranti);
mysql_free_result($rsoffre);
?>
<?php ob_end_flush();?>